﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;

public class Logger
{
    private static Logger __localShare = null;
    public static Logger Instance
    {
        get{ return __localShare; }
        set { __localShare = value; }
    }

    public static void CWriteLog(string format, params object[] args)
    {
        if (Instance != null)
        {
            Instance.WriteLog(format, args);
        }
    }

    public string Session { get; internal set; }

    private string FilePath = string.Empty;
    private object locker = string.Copy("locker");

    public bool ThrowErrorException { get; set; }

    public Logger(string filePath)
    {
        this.FilePath = filePath;
        ThrowErrorException = false;
    }

    public Logger(string filePath, string session):this(filePath)
    {
        Session = session;
    }

    public void WriteLog(string format, params object[] args)
    {
        try
        {
            lock (locker)//exclusive access
            {
                FileInfo info = new FileInfo(this.FilePath);
                if (info.Exists)
                {
                    string filename = Path.GetFileNameWithoutExtension(info.Name);
                    if (info.Length > 1024 * 1024) //1MB
                    {
                        info.MoveTo(Path.Combine(
                            info.DirectoryName,
                            string.Format("{0}-{1}{2}",
                                Path.GetFileNameWithoutExtension(info.Name),
                                DateTime.Now.ToString("MMMdd.hhmmtt"),
                                info.Extension)));
                    }
                }
                else
                {
                    if (!info.Directory.Exists)
                        info.Directory.Create();
                }

                using (StreamWriter logger = new StreamWriter(
                        this.FilePath,
                        true,
                        Encoding.UTF8))
                {
                    logger.WriteLine(
                        string.Format("[{0}] ", DateTime.Now.ToString("MMMdd-hh:mm:ss.fff tt")) +
                        string.Format(format, args));
                }
            }
        }
        catch (Exception ex)
        {
            if (ThrowErrorException)
                throw ex;
        }
    }

    public static string PrintObject(object obj)
    {
        if (obj == null)
            return string.Empty;

        StringBuilder s = new StringBuilder();

        Type ot = obj.GetType();
        s.Append(ot.Name);
        s.Append(">> ");
        foreach (PropertyInfo prop in ot.GetProperties())
        {
            try
            {
                s.AppendFormat("{0}: {1}, ", prop.Name, prop.GetValue(obj, null));
            }
            catch { }
        }

        return s.ToString();
    }
}